package com.nolon.flange.tools

import java.text.SimpleDateFormat
import java.util.*

fun String?.F(default: Float): Float = try {
    if (this.isNullOrEmpty()) 0f else this.toFloat()
} catch (e: Exception) {
    default
}

val String?.F: Float get() = if (this.isNullOrEmpty()) 0f else this.toFloat()
val String?.I: Int get() = if (this.isNullOrEmpty()) 0 else this.toInt()
fun String?.I(default: Int) = if (this.isNullOrEmpty()) default else this.toInt()


val Long?.toFriendlyDate: String
    get() {
        if (this == null) return "-"
        val createAt = Date(this)
        val millisecond = Date().time - createAt.time
        var second = millisecond / 1000
        if (second <= 0) {
            second = 0
        }
//        //*--------------微博体（标准）
//        interval = if (second == 0L) {
//            "刚刚"
//        } else if (second < 30) {
//            second.toString() + "秒以前"
//        } else if (second in 30..59) {
//            "半分钟前"
//        } else if (second >= 60 && second < 60 * 60) { //大于1分钟 小于1小时
//            val minute = second / 60
//            minute.toString() + "分钟前"
//        } else if (second >= 60 * 60 && second < 60 * 60 * 24) { //大于1小时 小于24小时
//            val hour = second / 60 / 60
//            if (hour <= 3) {
//                hour.toString() + "小时前"
//            } else {
//                "今天" + getFormatTime(createAt, "HH:mm")
//            }
//        } else if (second >= 60 * 60 * 24 && second <= 60 * 60 * 24 * 2) { //大于1D 小于2D
//            "昨天" + getFormatTime(createAt, "HH:mm")
//        } else if (second >= 60 * 60 * 24 * 2 && second <= 60 * 60 * 24 * 7) { //大于2D小时 小于 7天
//            val day = second / 60 / 60 / 24
//            day.toString() + "天前"
//        } else if (second <= 60 * 60 * 24 * 365 && second >= 60 * 60 * 24 * 7) { //大于7天小于365天
//            getFormatTime(createAt, "MM-dd HH:mm")
//        } else if (second >= 60 * 60 * 24 * 365) { //大于365天
//            getFormatTime(createAt, "yyyy-MM-dd HH:mm")
//        } else {
//            "0"
//        }
        val interval = when (second) {
            in 0..59 -> "刚刚"
            //大于1分钟 小于1小时
            in 60..60 * 60 -> (second / 60).toString() + "分钟前"
            in 60 * 60..60 * 60 * 24 -> { //大于1小时 小于24小时
                val hour = second / 60 / 60
                if (hour <= 3) {
                    hour.toString() + "小时前"
                } else {
                    "今天" + getFormatTime(createAt, "HH:mm")
                }
            }
            //大于1D 小于2D
            in 60 * 60 * 24..60 * 60 * 24 * 2 -> "昨天" + getFormatTime(createAt, "HH:mm")
            //大于2D小时 小于 7天
            in 60 * 60 * 24 * 2..60 * 60 * 24 * 7 -> (second / 60 / 60 / 24).toString() + "天前"
            //大于7天小于365天
            in 60 * 60 * 24 * 7..60 * 60 * 24 * 365 -> getFormatTime(createAt, "MM-dd HH:mm")
            //大于365天
            in 60 * 60 * 24 * 365..Long.MAX_VALUE -> getFormatTime(createAt, "yyyy-MM-dd HH:mm")
            else -> "0"
        }
        return interval
    }

fun getFormatTime(date: Date, Sdf: String?): String {
    return SimpleDateFormat(Sdf, Locale.getDefault()).format(date)
}